package com.proxy.daijia.driver.service.impl;

import com.proxy.daijia.common.constant.RedisConstant;
import com.proxy.daijia.common.execption.ProxyException;
import com.proxy.daijia.common.result.Result;
import com.proxy.daijia.common.result.ResultCodeEnum;
import com.proxy.daijia.dispatch.client.NewOrderFeignClient;
import com.proxy.daijia.driver.client.DriverInfoFeignClient;
import com.proxy.daijia.driver.service.DriverService;
import com.proxy.daijia.map.client.LocationFeignClient;
import com.proxy.daijia.model.form.driver.DriverFaceModelForm;
import com.proxy.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.proxy.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;

    @Autowired
    LocationFeignClient locationFeignClient;

    @Autowired
    NewOrderFeignClient newOrderFeignClient;

    //小程序授权登录
    @Override
    public String login(String code) {

        Result<Long> longResult = driverInfoFeignClient.login(code);
        if (longResult.getCode() != 200) {
            throw new ProxyException(ResultCodeEnum.DATA_ERROR);
        }

        Long driverId = longResult.getData();
        if (driverId == null) {
            throw new ProxyException(ResultCodeEnum.DATA_ERROR);
        }

        String token = UUID.randomUUID().toString().replace("-", "");

        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token, driverId.toString(), RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        return token;
    }

    //更新司机认证信息
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> result = driverInfoFeignClient.updateDriverAuthInfo(updateDriverAuthInfoForm);
        Boolean data = result.getData();
        return data;
    }

    //创建司机人脸模型
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm).getData();
    }


    //开始接单服务
    @Override
    public Boolean startService(Long driverId) {
        //判断司机是否认证
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
        if (driverLoginVo.getAuthStatus() != 2) {
            throw new ProxyException(ResultCodeEnum.AUTH_ERROR);
        }
        //判断司机是否人脸识别
        Boolean isFace = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if (!isFace) {
            throw new ProxyException(ResultCodeEnum.FACE_ERROR);
        }
        //更新司机接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 1);

        //删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        //删除司机临时队列订单数据
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }

    //停止接单服务
    @Override
    public Boolean stopService(Long driverId) {
        //更新司机的接单状态 0
        driverInfoFeignClient.updateServiceStatus(driverId,0);

        //删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        //清空司机临时队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

    //判断司机当日是否进行过人脸识别
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }
}
